package rexsee.core.browser.clazz;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.ActivityResult.ActivityResultListener;
import rexsee.core.lang.RexseeLanguage;
import android.content.Intent;

public class SyncTask {

	private boolean mSyncTag = false;
	private Thread mThread;

	private String mStringRtn = null;
	private boolean mBooleanRtn = false;
	private int mIntRtn = -1;
	private ActivityReturn mActivityReturn = null;

	public void run(final Runnable runnable, Browser browser, boolean cancelable) {
		if (browser != null) {
			if (cancelable) {
				browser.progressDialog.showWithRunnable(RexseeLanguage.PROGRESS_ONGOING, new Runnable() {
					@Override
					public void run() {
						mThread = null;
						mSyncTag = true;
					}
				});
			} else {
				browser.progressDialog.show(RexseeLanguage.PROGRESS_ONGOING);
			}
		}
		mThread = new Thread() {
			@Override
			public void run() {
				mSyncTag = true;
			}
		};
		mSyncTag = false;
		mThread.start();
		while (!mSyncTag) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
			}
		}
		mThread = null;
		if (browser != null) browser.progressDialog.hide();
		return;
	}
	public String run(final StringRunnable runnable, Browser browser, boolean cancelable) {
		if (browser != null) {
			if (cancelable) {
				browser.progressDialog.showWithRunnable(RexseeLanguage.PROGRESS_ONGOING, new Runnable() {
					@Override
					public void run() {
						mThread = null;
						mStringRtn = null;
						mSyncTag = true;
					}
				});
			} else {
				browser.progressDialog.show(RexseeLanguage.PROGRESS_ONGOING);
			}
		}
		mThread = new Thread() {
			@Override
			public void run() {
				mStringRtn = runnable.run();
				mSyncTag = true;
			}
		};
		mStringRtn = null;
		mSyncTag = false;
		mThread.start();
		while (!mSyncTag) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
			}
		}
		mThread = null;
		if (browser != null) browser.progressDialog.hide();
		return mStringRtn;
	}
	public boolean run(final BooleanRunnable runnable, Browser browser, boolean cancelable) {
		if (browser != null) {
			if (cancelable) {
				browser.progressDialog.showWithRunnable(RexseeLanguage.PROGRESS_ONGOING, new Runnable() {
					@Override
					public void run() {
						mThread = null;
						mBooleanRtn = false;
						mSyncTag = true;
					}
				});
			} else {
				browser.progressDialog.show(RexseeLanguage.PROGRESS_ONGOING);
			}
		}
		mThread = new Thread() {
			@Override
			public void run() {
				mBooleanRtn = runnable.run();
				mSyncTag = true;
			}
		};
		mBooleanRtn = false;
		mSyncTag = false;
		mThread.start();
		while (!mSyncTag) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
			}
		}
		mThread = null;
		if (browser != null) browser.progressDialog.hide();
		return mBooleanRtn;
	}
	public int run(final IntRunnable runnable, Browser browser, boolean cancelable) {
		if (browser != null) {
			if (cancelable) {
				browser.progressDialog.showWithRunnable(RexseeLanguage.PROGRESS_ONGOING, new Runnable() {
					@Override
					public void run() {
						mThread = null;
						mIntRtn = -1;
						mSyncTag = true;
					}
				});
			} else {
				browser.progressDialog.show(RexseeLanguage.PROGRESS_ONGOING);
			}
		}
		mThread = new Thread() {
			@Override
			public void run() {
				mIntRtn = runnable.run();
				mSyncTag = true;
			}
		};
		mIntRtn = -1;
		mSyncTag = false;
		mThread.start();
		while (!mSyncTag) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
			}
		}
		mThread = null;
		if (browser != null) browser.progressDialog.hide();
		return mIntRtn;
	}

	public ActivityReturn runActivity(final Intent startIntent, Browser browser) {
		mActivityReturn = null;
		mSyncTag = false;
		browser.activityResult.start(startIntent, new ActivityResultListener() {
			@Override
			public void run(int resultCode, Intent resultIntent) {
				mActivityReturn = new ActivityReturn(resultCode, resultIntent);
				mSyncTag = true;
			}
		});
		while (!mSyncTag) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
			}
		}
		return mActivityReturn;
	}

	public static abstract class StringRunnable {
		public abstract String run();
	}
	public static abstract class BooleanRunnable {
		public abstract boolean run();
	}
	public static abstract class IntRunnable {
		public abstract int run();
	}

}
